//
//  LinnerLightSetViewController.m
//  LINNER
//
//  Created by wei wang on 2017/4/18.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "LinnerLightSetViewController.h"

@interface LinnerLightSetViewController ()<UITableViewDelegate,UITableViewDataSource>
/** lightArray **/
@property(nonatomic,strong) NSArray *lightArray;
@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic,assign) NSInteger selectIndex;
@property(nonatomic,strong) NSArray *dataArray;

@end

@implementation LinnerLightSetViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.selectIndex = [AppDelegate globalDelegate].GetheadsetLight;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNavigationStyle];
    self.lightArray = @[@"默认",@"常亮",@"呼吸灯",@"关闭"];
    self.dataArray = @[@"aa01022e02",@"aa01022e01",@"aa01022e03",@"aa01022e00"];
    
    [self masLayoutSubview];
}

- (void)setNavigationStyle{
    
    [self setNavigationBarTitle:MyNSLocalizedString(@"装饰灯设置")];
    [self setLeftButtonImage:[UIImage imageNamed:@"tittle_back_nor"]];
    [self.navigationController.navigationBar makeTransparent_nornav];
}

#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.lightArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.textLabel.textColor = [UIColor colorWithHexString:@"#56504f"];
        [cell.textLabel setFont:[UIFont systemFontOfSize:15.f]];

    }
    cell.textLabel.text = MyNSLocalizedString(self.lightArray[indexPath.row]);
    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    cell.accessoryView = image;
    image.image = [UIImage imageNamed:@"icon_choice"];
    
    cell.accessoryView.hidden = self.selectIndex == indexPath.row ? NO :YES;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 56;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}
#pragma mark - --------------------------UITableView delegate--------------------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    self.selectIndex = indexPath.row;
    
    [self sendData:indexPath.row];
    
    [self.tableView reloadData];
    
    
}

- (void)sendData:(NSInteger)indexTag{
    
    NSString *sendata = self.dataArray[indexTag];
    const char *buf = [sendata UTF8String];
    NSMutableData *data = [NSMutableData data];
    if (buf)
    {
        uint32_t len = (uint32_t)strlen(buf);
        
        char singleNumberString[3] = {'\0', '\0', '\0'};
        uint32_t singleNumber = 0;
        for(uint32_t i = 0 ; i < len; i+=2)
        {
            if ( ((i+1) < len) && isxdigit(buf[i]) && (isxdigit(buf[i+1])) )
            {
                singleNumberString[0] = buf[i];
                singleNumberString[1] = buf[i + 1];
                sscanf(singleNumberString, "%x", &singleNumber);
                uint8_t tmp = (uint8_t)(singleNumber & 0x000000FF);
                [data appendBytes:(void *)(&tmp) length:1];
            }
            else
            {
                break;
            }
        }
        
        NSLog(@"发送%@",data);
        [[EADSessionController sharedController] writeData:data];
    }

}


- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        //        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];

    }
    
    return _tableView;
    
}

- (void)masLayoutSubview{
    
    
    self.tableView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
